#!/usr/bin/env/python
# import sip
# sip.setapi('QVariant', 2)

import os
import sys
import StringIO
import PySide
from PySide.QtGui import *
from PySide.QtCore import *
sys.modules['PyQt4'] = PySide
from PIL import Image, ImageQt

class CommonFunctions():
    def __init__(self):
        self.debugMode = True

    def opj(self, path):
        # converts path to the platform-specific separator
        st = apply(os.path.join, tuple(path.split('/')))
        # hack: on linux, a leading / gets lost
        if path.startswith('/'):
            st = '/' + st
        return st

    def getAppPath(self):
        # get path to this script no matter how its run
        # determine if app is py/pyw or frozen exe
        if hasattr(sys, 'frozen'):
            appPath = os.path.dirname(sys.executable)
        elif '__file__' in locals():
            appPath = os.path.dirname(__file__)
        else:
            appPath = sys.path[0]
        return appPath   
    
    def displayError(self, title, text, errortype):
        if errortype == "warning":
            errMsg = QMessageBox(QMessageBox.Warning, title, text)
        elif errortype == "critical":
            errMsg = QMessageBox(QMessageBox.Critical, title, text)
        else:
            displayDebug("invalid errotype provided")
        errMsg.setStandardButtons(QMessageBox.Ok)
        errMsg.exec_()

    def displayDebug(self, msg):
        if(self.debugMode == True):
            print msg

    def makePixmapFromString(self, imagedata):
        img = QImage()
        img.loadFromData(QByteArray(imagedata))
        pix = QPixmap.fromImage(img)
        return pix
    
    def makePixmapFromPIL(self, pimg):
        qimg = QImage(ImageQt.ImageQt(pimg))
        pix = QPixmap.fromImage(qimg)
        return pix
    
    def removeduplicates(self, list):
        seen = {}
        pos = 0
        for item in list:
            if item not in seen:
                seen[item] = True
                list[pos] = item
                pos += 1
        del list[pos:]
